﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileChangeRegistration.cs" company="" />
// <summary>
//   Maintains the relation between a file-system watcher (producer) and a list of consumers that needs notification.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Utilities.FileChangesTracking
{
    using System;
    using System.Collections.Generic;
    using System.IO;

    /// <summary>
    /// Defines a delegate for a consumer that needs to know of file changes.
    /// </summary>
    /// <param name="changeType">The type of the change.</param>
    /// <param name="changedPath">The path of the file changed.</param>
    /// <param name="newPath">If not 'null' - the change operation was rename, and this is the new name of the file.</param>
    public delegate void FileChangeHandler(FileChangeType changeType, string changedPath, string newPath);

    /// <summary>
    /// Maintains the relation between a file-system watcher (producer) and a list of consumers that needs notification.
    /// </summary>
    public class FileChangeSubscription
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FileChangeSubscription"/> class with a defined file-system watcher.
        /// </summary>
        /// <param name="watcher">The file-system watcher producer to set.</param>
        public FileChangeSubscription(FileSystemWatcher watcher)
        {
            Watcher = watcher;
            Handlers = new Dictionary<Guid, FileChangeHandlerInfo>();
        }

        /// <summary>
        /// Gets the file-system watcher associated as a producer.
        /// </summary>
        public FileSystemWatcher Watcher { get; private set; }

        /// <summary>
        /// Gets the list of consumers' reaction delegates (handlers) to a file changes.
        /// </summary>
        public Dictionary<Guid, FileChangeHandlerInfo> Handlers { get; private set; }
    }

    /// <summary>
    /// Maintains internal information about the file change handler.
    /// </summary>
    public class FileChangeHandlerInfo
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FileChangeHandlerInfo"/> class.
        /// </summary>
        /// <param name="fileName">The file name associated with this handler.</param>
        /// <param name="handler">The handler itself.</param>
        public FileChangeHandlerInfo(string fileName, FileChangeHandler handler)
        {
            FileName = fileName;
            Handler = handler;
        }

        /// <summary>
        /// Gets the name of the file handled by this handler
        /// </summary>
        public string FileName { get; private set; }

        /// <summary>
        /// Gets the change handler itself.
        /// </summary>
        public FileChangeHandler Handler { get; private set; }
    }
}
